package top.lingkang.potato.config;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;

public class UserHomeConfig {
    private static final Log log = LogFactory.get();
    private static String userHomePath;
    private static Properties configProperties;
    private static Properties userProperties;
    private static final String CONFIG_FILE = "config.properties";

    public static String getUserHomePath() {
        if (StrUtil.isEmpty(userHomePath)) {
            userHomePath = System.getProperty("user.home");
        }
        return userHomePath;
    }

    /**
     * example: C:\Users\Administrator\potato
     *
     * @return user.home\potato
     */
    public static String getHomePotatoPath() {
        File file = new File(getUserHomePath() + File.separator + "potato");
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getPath();
    }

    public static Properties getProperty() {
        if (configProperties == null) {
            File file = new File(getHomePotatoPath() + File.separator + CONFIG_FILE);
            if (!file.exists()) {
                try {
                    log.debug("config file not exists! mkdirs");
                    file.createNewFile();
                } catch (IOException e) {
                    log.error(e);
                }
            }
            configProperties = new Properties();
            try {
                FileReader fileReader = new FileReader(file);
                configProperties.load(new InputStreamReader(fileReader.getInputStream(),"utf-8"));
            } catch (IOException e) {
                log.error(e);
            }
        }
        return configProperties;
    }

    public static void setProperties(String key, String value) {
        Properties property = getProperty();
        property.setProperty(key, value);
        try {
            log.debug("config file set k-v");
            FileWriter writer = new FileWriter(getHomePotatoPath() + File.separator + CONFIG_FILE);
            property.store(new OutputStreamWriter(writer.getOutputStream(), "utf-8"), "set k-v");
            configProperties = property;
        } catch (Exception e) {
            log.error(e);
        }
    }

    public static void setUserProperties(String username, String json) {
        Properties properties = getUserProperties(username);
        properties.setProperty("info", json);
        try {
            log.debug("config file set k-v");
            FileWriter writer = new FileWriter(getUserPropertiesFile(username).getPath());
            properties.store(new OutputStreamWriter(writer.getOutputStream(),"utf-8"), "set k-v");
            userProperties = properties;
        } catch (Exception e) {
            log.error(e);
        }
    }

    private static File getUserPropertiesFile(String username) {
        File file = new File(getHomePotatoPath() + File.separator + username + ".properties");
        if (!file.exists()) {
            try {
                log.debug(username + ".properties file not exists! createNewFile");
                file.createNewFile();
            } catch (IOException e) {
                log.error(e);
            }
        }
        return file;
    }

    private static Properties getUserProperties(String username) {
        if (userProperties == null) {
            userProperties = new Properties();
            try {
                FileReader reader = new FileReader(getUserPropertiesFile(username));
                userProperties.load(new InputStreamReader(reader.getInputStream(),"utf-8"));
            } catch (IOException e) {
                log.error(e);
            }
        }
        return userProperties;
    }
}
